package org.pisces.wechat.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.pisces.wechat.commons.WechatConstants;
import org.pisces.wechat.commons.utils.HttpClientUtils;
import org.pisces.wechat.mapper.UserMapper;
import org.pisces.wechat.po.User;
import org.pisces.wechat.service.api.IUserService;
import org.pisces.wechat.service.utils.WechatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class UserService extends BaseService implements IUserService{

	@Autowired
	private UserMapper userMapper;
	
	@Value("${wechat.app.secret}")
	private String secret;
	
	@Override
	public void subscribe(String fromUserName) {
		Example example = new Example(User.class);
		example.createCriteria().andEqualTo("openId", fromUserName);
		int count = userMapper.selectCountByExample(example);
		if(count==0) {
			User user = getUserByWechat(fromUserName);
			userMapper.insertSelective(user);
		}
	}
	
	private User getUserByWechat(String openId) {
		User user = new User();
		user.setOpenId(openId);
		try {
			JSONObject userInfo = WechatUtils.getUserInfo(WechatConstants.APP_ID, secret, openId);
			if(userInfo.containsKey("nickname")) {
				user.setNickname(userInfo.getString("nickname"));
			}
			if(userInfo.containsKey("language")) {
				user.setLanguage(userInfo.getString("language"));
			}
			if(userInfo.containsKey("subscribe")) {
				user.setSubscribe(userInfo.getInteger("subscribe"));
			}
			if(userInfo.containsKey("sex")) {
				user.setSex(userInfo.getInteger("sex"));
			}
			if(userInfo.containsKey("city")) {
				user.setCity(userInfo.getString("city"));
			}
			if(userInfo.containsKey("province")) {
				user.setProvince(userInfo.getString("province"));
			}
			if(userInfo.containsKey("country")) {
				user.setCountry(userInfo.getString("country"));
			}
			if(userInfo.containsKey("headimgurl")) {
				user.setHeadimgurl(userInfo.getString("headimgurl"));
			}
			if(userInfo.containsKey("subscribe_time")) {
				user.setSubscribeTime(userInfo.getLong("subscribe_time"));
			}
			if(userInfo.containsKey("unionid")) {
				user.setUnionid(userInfo.getString("unionid"));
			}
			if(userInfo.containsKey("remark")) {
				user.setRemark(userInfo.getString("remark"));
			}
			
			if(userInfo.containsKey("tagid_list")) {
				user.setTagidList(userInfo.getString("tagid_list"));
			}
			
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		return user;
	}

	@Override
	public String username(String openId) {
		
		
		return null;
	}
	
	
	@Override
	public JSONObject getAccessTokenByCode(String code) {
		String urls =  "https://api.weixin.qq.com/sns/oauth2/access_token";
		Map<String, String> map = new HashMap<String, String>();
		map.put("appid", "wx66e4e106a9a621f8"); // 公众号的唯一标识
		map.put("secret", "32f1b0ab4f521dc741c3844b2e4d79c9"); // 公众号的appsecret
		map.put("code", code); // 填写第一步获取的code参数
		map.put("grant_type", "authorization_code"); // 填写为authorization_code
		String doPost = null;
		try {
			doPost = HttpClientUtils.postParameters(urls, map);
		} catch (Exception e) {
			return new JSONObject();
		}
		JSONObject json = JSONObject.parseObject(doPost);
		logger.info(json.toString());
		return json;

	}

}
